# -*- coding: utf-8 -*-
##############################################################################
#
#    OpenERP, Open Source Management Solution
#    Copyright (C) 2004-2010 Tiny SPRL (<http://tiny.be>).
#
#    This program is free software: you can redistribute it and/or modify
#    it under the terms of the GNU Affero General Public License as
#    published by the Free Software Foundation, either version 3 of the
#    License, or (at your option) any later version.
#
#    This program is distributed in the hope that it will be useful,
#    but WITHOUT ANY WARRANTY; without even the implied warranty of
#    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
#    GNU Affero General Public License for more details.
#
#    You should have received a copy of the GNU Affero General Public License
#    along with this program.  If not, see <http://www.gnu.org/licenses/>.
#
##############################################################################

from openerp import addons
import logging
from openerp.osv import fields, osv
from openerp.tools.translate import _
from openerp import tools

class hr_employee(osv.osv):
    _inherit = "hr.employee"
    _columns = {
        'attendance_code': fields.many2one('hrm2.attendance.code','Attendance Code',domain="[('device_id','=',device_id)]",required=True),
        'device_id': fields.many2one('hr.device','Device',required=True),
    }
    _defaults = {

    }
    def _check_device(self, cr, uid, ids):
        for device in self.browse(cr, uid, ids):
            device_id=device.device_id.id
            attendance_id=device.attendance_code.id
            device_ids = self.search(cr, uid, [('id','<>',device.id), ('device_id', '=',device_id), ('attendance_code','=',attendance_id)])
            if device_ids:
                raise osv.except_osv(_('Invalid Action!'), _('The Attendance Code \'%s\' exist in this Device \'%s\'.') %(device.attendance_code.attendance_id,device.device_id.name,))
                return False
        return True
    _constraints = [
        (_check_device, '', ['device_id','attendance_code']),
    ]
    
hr_employee()